package icu.d4peng.cloud.common.log.properties;

import com.alibaba.fastjson.JSON;
import icu.d4peng.cloud.common.log.context.LogContext;
import icu.d4peng.cloud.common.log.generator.spanid.DefaultSpanIdGenerator;
import icu.d4peng.cloud.common.log.generator.spanid.SpanIdGenerator;
import icu.d4peng.cloud.common.log.generator.traceid.DefaultTraceIdGenerator;
import icu.d4peng.cloud.common.log.generator.traceid.TraceIdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author d4peng
 * @version 1.0.0
 * @date 2021-12-27 13:47
 * @description LogProperties:日志配置
 */
@ConfigurationProperties(prefix = LogProperties.PREFIX)
public class LogProperties implements InitializingBean {
    /**
     * 前缀:roc.log
     */
    public static final String PREFIX = "roc.log";
    /**
     * 是否开启:默认开启
     */
    private boolean enabled = true;
    /**
     * 是否开启MDC:上下文映射
     */
    private boolean enableMdc = true;
    /**
     * MDC模式串:默认为<traceId:$traceId><spanId:$spanId><currentIp:$currentIp><preApp:$preApp><preHost:$preHost><preIp:$preIp>
     */
    private String MdcPattern = "<traceId:$traceId><spanId:$spanId><currentIp:$currentIp><preApp:$preApp><preHost:$preHost><preIp:$preIp>";
    /**
     * traceId生成器:为空就使用默认的生成器
     */
    private Class<? extends TraceIdGenerator> traceIdGenerator = DefaultTraceIdGenerator.class;
    /**
     * spanId生成器:为空就使用默认的
     */
    private Class<? extends SpanIdGenerator> spanIdGenerator = DefaultSpanIdGenerator.class;
    /**
     * 是否开启aop记录:默认不开启
     */
    private boolean enableAopLog = false;
    /**
     * 是否开启HTTP拦截:默认不开启
     */
    private boolean enableHttpLog = false;

    public boolean isEnabled() {
        return enabled;
    }

    public LogProperties setEnabled(boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    public boolean isEnableMdc() {
        return enableMdc;
    }

    public LogProperties setEnableMdc(boolean enableMdc) {
        this.enableMdc = enableMdc;
        return this;
    }

    public String getMdcPattern() {
        return MdcPattern;
    }

    public LogProperties setMdcPattern(String mdcPattern) {
        MdcPattern = mdcPattern;
        return this;
    }

    public Class<? extends TraceIdGenerator> getTraceIdGenerator() {
        return traceIdGenerator;
    }

    public LogProperties setTraceIdGenerator(Class<? extends TraceIdGenerator> traceIdGenerator) {
        this.traceIdGenerator = traceIdGenerator;
        return this;
    }

    public Class<? extends SpanIdGenerator> getSpanIdGenerator() {
        return spanIdGenerator;
    }

    public LogProperties setSpanIdGenerator(Class<? extends SpanIdGenerator> spanIdGenerator) {
        this.spanIdGenerator = spanIdGenerator;
        return this;
    }

    public boolean isEnableAopLog() {
        return enableAopLog;
    }

    public LogProperties setEnableAopLog(boolean enableAopLog) {
        this.enableAopLog = enableAopLog;
        return this;
    }

    public boolean isEnableHttpLog() {
        return enableHttpLog;
    }

    public LogProperties setEnableHttpLog(boolean enableHttpLog) {
        this.enableHttpLog = enableHttpLog;
        return this;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        // 设置是否开启MDC
        LogContext.setEnableMDC(this.enableMdc);
        if (this.enableMdc) {
            // 设置MDC的正则表达式
            if (StringUtils.isNotEmpty(this.MdcPattern)) {
                LogContext.setMDCPattern(this.MdcPattern);
            }
            // 设置traceId生成器
            LogContext.setTraceIdGenerator(this.traceIdGenerator.newInstance());
            // 设置spanId生成器
            LogContext.setSpanIdGenerator(this.spanIdGenerator.newInstance());
        }
    }
}
